package com.wzz.auth.cust;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wzz.auth.cache.Cache;

@Controller
public class CustController {

	@Autowired
	Cache cache;

	@RequestMapping("/cust/action")
	public void deal(HttpServletRequest request, HttpServletResponse response, String name, String data) {
		System.out.println("客户来了。。。");
		System.out.println("name=" + name);
		if (cache.getMap().get(name+"_token") == null) {
			System.out.println("没有token...");
			try {
				cache.getMap().put(name+"_name", name);
				cache.getMap().put(name+"_data", data);

				String param = "";
				param += "response_type=" + "code&";
				param += "client_id=" + "1000&";
				param += "redirect_uri=" + "http://localhost:8080/AuthCust/cust/sqm&";
				param += "scope=" + "1&";
				param += "state=" + name;
				response.sendRedirect("http://localhost:8080/AuthServer/server/accept?" + param);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println(cache.getMap().get(name+"_token"));
		}

	}

	@RequestMapping("/cust/sqm")
	public void dealShouQuanMa(String code, String name, String state, String access_token, HttpServletResponse response) {
		if (code != null && !"".equals(code)) {
			System.out.println("--------------");
			System.out.println("接收授权码");
			System.out.println("code=" + code);
			System.out.println("name=" + name);

			try {
				response.sendRedirect("http://localhost:8080/AuthServer/server/token?code=" + code + "&redirect_uri="
						+ "http://localhost:8080/AuthCust/cust/sqm&state="+name);
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			name = state;
			System.out.println("--------------");
			System.out.println("接收token");
			System.out.println("name="+name);
			System.out.println("token=" + access_token);
			
			cache.getMap().put(name+"_token", access_token);
			
			
			cache.getMap().get(name+"_data");
			try {
				response.sendRedirect("http://localhost:8080/AuthCust/cust/action?name="+name);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
		}

	}
}
